/**
 * LoginAction.java
 * cn.feigme.identity.web.action
 * @author Feigme 
 * 2008-2-9
 */
package cn.feigme.identity.web.action;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import cn.feigme.identity.model.LoginHistory;
import cn.feigme.identity.model.User;
import cn.feigme.identity.service.UserService;

/**
 * @author Feigme
 * 
 */
public class IdentityAction extends IdentitySupport {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(IdentityAction.class);
	
	private UserService userService;

	public String execute() throws Exception {
		return null;
	}

	public String login()throws Exception{
		logger.info("execute...login");

		List<User> users = this.userService.login(getUser().getEmail(), getUser().getPassword());
		if(users.size()==0 || users.size()>1){
			setMessage(getText("identity.noUser"));
			return INPUT;
		}else{
			User user = users.get(0);
			if(user.getEnabled().booleanValue()){
				return SUCCESS;
			}else{
				setMessage(getText("identity.noValid"));
				return INPUT;
			}
		}
	}
	
	public String register()throws Exception{
		logger.info("execute...register");
		
		/**
		 *非IOC的方式得到 request，response session
	     *HttpServletRequest request = ServletActionContext.getRequest();
	     *HttpServletResponse response = ServletActionContext.getResponse();
	     *HttpSession session = request.getSession();
	     */

		/**
		 * IOC的方式得到request，response session 要实现implements SessionAware, ServletRequestAware, ServletResponseAware
		 * private HttpServletRequest request;
   		 * private HttpServletResponse response; 
   		 * 
		 */
		
		HttpServletRequest request = ServletActionContext.getRequest();
		logger.info("111111111"+request.getParameter("user.question_select_input"));
		logger.info("22222222"+user.getQuestion());
		user.setEnabled(Boolean.FALSE);
		
		Set<LoginHistory> set = new HashSet<LoginHistory>();
		LoginHistory loginHistory = new LoginHistory();
		loginHistory.setLoginAddress(request.getRemoteAddr());
		loginHistory.setUser(user);
		set.add(loginHistory);
		user.setLoginHistorys(set);
		//this.getUserService().register(user);
		return SUCCESS;
	}
	
	public void check(){
		logger.info("check useremail exist!");
		HttpServletResponse response = ServletActionContext.getResponse();    
		HttpServletRequest request = ServletActionContext.getRequest();    
		response.setContentType("text/xml;charset=utf-8");    
		response.setHeader("Cache-Control","no-cache");    
		   
		String email = request.getParameter("user.email");
		if(getUserService().checkUserExist(email)){
			setMessage("youyouyou");
		}else{
			setMessage("meimeimei");
		}		
		
		try {
			Thread.sleep(2000);
			response.getWriter().print(getMessage());
			response.getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
		}     

	}

	private User user;
	private String confirmPassword;
	private String message;

	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

}
